/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package csvconvertor;

import java.io.FileNotFoundException;

/**
 *
 * @author sajin
 */
public class TypeOfCSV {
    public TypeOfCSV(String headerTypeFileLoc) {
        this.headerTypeFileLoc = headerTypeFileLoc;
    }

    public String findType(String[] headers) {
        boolean foundType = false;
        String[] KnownHeader;
        CSVReader csvRd;
        String csvType = "" ;
        int i,j;

        try {
            csvRd = new CSVReader(headerTypeFileLoc);
            KnownHeader = csvRd.getHeaderFields();

            while (!foundType && ( KnownHeader  != null ) )  {
                foundType = true; // Assume type is found. Check correctness of assumption.
                for ( i = 0 , j = 1; i < headers.length && j < KnownHeader.length  ; i++,j++ ) {
                    if( ! headers[i].equals( KnownHeader[j] ) )
                        foundType = false;
                }
                if(foundType && ( ( i == headers.length ) && ( j == KnownHeader.length )) )
                    csvType = KnownHeader[0];
                else {
                    foundType = false;
                    KnownHeader = csvRd.getFields();
                }
            }
        } catch (FileNotFoundException Fexp) {
            System.err.println("Types file not found :" + Fexp.getMessage());
            csvType = "Unknown" ;
        }
        
        if ( ! foundType  )
            csvType = "Unknown";


        return csvType;
    }

    protected String[] headers;
    protected String headerTypeFileLoc;
}

